package com.itheima.listener;

import com.itheima.domain.mongo.MovementScore;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@RocketMQMessageListener(topic = "recommend-movement", consumerGroup = "recommend-movement-consumer-group")
public class MovementScoreListener implements RocketMQListener<Map> {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(Map map) {
        //1. 获取接收到的参数
        Long userId = ((Integer) map.get("userId")).longValue();
        Long pid = ((Integer) map.get("pid")).longValue();
        Integer type = (Integer) map.get("type");

        //2. 组装movementScore对象
        MovementScore movementScore = new MovementScore();
        movementScore.setUserId(userId);
        movementScore.setPublishId(pid);
        movementScore.setDate(System.currentTimeMillis());

        // 得分要根据类型计算
        //  1-发动态，2-浏览动态，3-点赞，4-喜欢，5-评论，6-取消点赞，7-取消喜欢
        switch (type) {
            case 1: {
                movementScore.setScore(20d);
                break;
            }
            case 2: {
                movementScore.setScore(1d);
                break;
            }
            case 3: {
                movementScore.setScore(5d);
                break;
            }
            case 4: {
                movementScore.setScore(8d);
                break;
            }
            case 5: {
                movementScore.setScore(10d);
                break;
            }
            case 6: {
                movementScore.setScore(-5d);
                break;
            }
            case 7: {
                movementScore.setScore(-8d);
                break;
            }
        }

        //3. 保存
        mongoTemplate.save(movementScore);
    }
}
